// DeviceController.java
package com.ny.controller;

import com.ny.entity.Device;
import com.ny.entity.Result;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    
    @Autowired
    private DeviceService deviceService;
    
    // 查询所有设备信息
    @GetMapping("/findAll")
    public Result findAll() {
        return Result.success(deviceService.findAllDevices());
    }
    
    // 根据设备名称查询设备信息（单条件查询）
    @GetMapping("/findByName")
    public Result findByName(@RequestParam String name) {
        Device device = deviceService.findDeviceByName(name);
        if (device != null) {
            return Result.success(device);
        } else {
            return Result.error("设备不存在：" + name);
        }
    }
    
    // 模糊查询设备信息
    @GetMapping("/search")
    public Result search(@RequestParam String name) {
        List<Device> devices = deviceService.findDevicesByName(name);
        if (devices != null && !devices.isEmpty()) {
            return Result.success(devices);
        } else {
            return Result.error("未找到包含 '" + name + "' 的设备");
        }
    }
    
    // 查询某个时间段且状态是离线的设备信息（多条件查询）
    @GetMapping("/offline")
    public Result findOfflineDevices(
        @RequestParam LocalDateTime startTime, 
        @RequestParam LocalDateTime endTime
    ) {
        List<Device> devices = deviceService.findOfflineDevicesByTime(startTime, endTime);
        return Result.success(devices);
    }
    
    // 查询某个时间段且状态是特定值的设备信息（多条件查询）
    // DeviceController.java


    
    // 根据设备id删除设备
    @DeleteMapping("/delete")
    public Result deleteDevice(@RequestParam Integer deviceId) {
        if (deviceService.deleteDeviceById(deviceId)) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    // DeviceController.java
    // DeviceController.java
@GetMapping("/findByCreatedTime")
public Result findDevicesByCreatedTime(
    @RequestParam(required = true) Date createdTime
) {
    List<Device> devices = deviceService.findDevicesByCreatedTimeAfter(createdTime);
    return Result.success(devices);
}

}
